package wqh.controller.json;

import cn.tpson.modbus.core.ModBusDevice;
import cn.tpson.modbus.core.ModBusManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.WaterDevice;
import wqh.global.GlobalTime;
import wqh.pojo.MapRet;
import wqh.service.WaterDeviceService;

/**
 * Created by devts on 2017/1/5.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/water_device")
public class WaterDeviceJSON {

    @Autowired
    private WaterDeviceService waterDeviceService;

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(){
        return MapRet.DATA(waterDeviceService.findAll());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody WaterDevice w){
        try {
            waterDeviceService.save(w);

            ModBusDevice modBusDevice = new ModBusDevice(w.getIp(),w.getPort(), GlobalTime.seconds);
            modBusDevice.setAddress((byte)1);
            ModBusManager.getInstance().addDevice(modBusDevice);

            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return  MapRet.SQL_INSERT_ERROR();
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody WaterDevice w){
        try {
            WaterDevice waterDevice = waterDeviceService.findById(w.getId());
            if(waterDevice == null) return  MapRet.SQL_UPDATE_ERROR();
            if(w.getIp() != null || w.getPort() != null){
                String ip = w.getIp() == null ? waterDevice.getIp() : w.getIp();
                int port = w.getPort() == null ? waterDevice.getPort() : w.getPort();
                ModBusDevice modBusDevice = new ModBusDevice(ip,port, GlobalTime.seconds);
                modBusDevice.setAddress((byte)1);
                ModBusManager.getInstance().addDevice(modBusDevice);
                ModBusManager.getInstance().removeDevice(waterDevice.getIp(),waterDevice.getPort());
            }
            if(w.getDevice_id() != null) waterDevice.setDevice_id(w.getDevice_id());
            if(w.getIp() != null) waterDevice.setIp(w.getIp());
            if(w.getName() != null) waterDevice.setName(w.getName());
            if(w.getPort() != null) waterDevice.setPort(w.getPort());
            waterDeviceService.update(w);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return  MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestBody WaterDevice w){
        try {
            WaterDevice waterDevice = waterDeviceService.findById(w.getId());
            ModBusManager.getInstance().removeDevice(waterDevice.getIp(),waterDevice.getPort());
            waterDeviceService.delete(w);
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return  MapRet.SQL_DELETE_ERROR();
        }
    }

}
